/*
 * @Author: 烟浮
 * @Date: 2022-06-26 01:06:18
 * @LastEditTime: 2022-06-26 02:11:31
 * @LastEditors: 锦墨涵秋 275740441@qq.com
 * @FilePath: /vite-ts-vue3/src/store/products.ts
 */
import { defineStore } from "pinia";
import { getProducts, IProduct } from '../api/shop'

export const useProductsStore = defineStore('products', {
    state: () => {
        return {
            // 所有的商品列表
            all: [] as IProduct[]
        }
    },
    getters: {},
    actions: {
        async loadAllProducts() {
            const productsResult = await getProducts()
            // console.log(productsResult);
            this.all = productsResult
        },
        decrementProduct(product: IProduct) {
            const ret = this.all.find(item => item.id === product.id)
            if (ret) {
                ret.inventory--
            }
        }
    }
})